<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="redis唯一标志" label-width="110px" prop="redisId">
        <el-input
          v-model="queryParams.redisId"
          placeholder="请输入redis唯一标志"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="访问地址" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入访问地址"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配置说明" prop="bz">
        <el-input
          v-model="queryParams.bz"
          placeholder="请输入配置说明"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="redisconfigList" height="330px">
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ (pageParams.current-1)*pageParams.size + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="redis唯一标志" align="center" prop="redisId" />
      <el-table-column label="访问地址" align="center" prop="ip" />
      <el-table-column label="端口" align="center" prop="port" />
      <el-table-column label="最大连接数" align="center" prop="maxTotal" />
      <el-table-column label="最小连接数" align="center" prop="maxIdle" />
      <el-table-column label="连接空闲时间" align="center" prop="maxWaitMillis" />
      <el-table-column label="请求超时时间" align="center" prop="timeout" />
      <el-table-column label="配置说明" align="center" prop="bz" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:redisconfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleTest(scope.row)"
            v-hasPermi="['system:redisconfig:list']"
          >测试</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageParams.current"
      :limit.sync="pageParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改redis配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="redis唯一标志" prop="redisId">{{ form.redisId }}</el-form-item>
        <el-form-item label="访问地址" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入访问地址" />
        </el-form-item>
        <el-form-item label="redis端口" prop="port">
          <el-input v-model="form.port" placeholder="请输入端口" />
        </el-form-item>
        <el-form-item label="redis密码" prop="pwd">
          <el-input v-model="form.pwd" type="password" show-password placeholder="请输入redis密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="qrpwd">
          <el-input v-model="form.qrpwd" type="password" show-password placeholder="请输入再次输入redis密码" />
        </el-form-item>
        <el-form-item label="最大连接数" prop="maxTotal">
          <el-input v-model="form.maxTotal" placeholder="请输入最大连接数" />
        </el-form-item>
        <el-form-item label="最小连接数" prop="maxIdle">
          <el-input v-model="form.maxIdle" placeholder="请输入最小连接数" />
        </el-form-item>
        <el-form-item label="连接空闲时间(ms)" prop="maxWaitMillis">
          <el-input v-model="form.maxWaitMillis" placeholder="请输入连接空闲时间" />
        </el-form-item>
        <el-form-item label="请求超时时间(s)" prop="timeout">
          <el-input v-model="form.timeout" placeholder="请输入请求超时时间" />
        </el-form-item>
        <el-form-item label="配置说明" prop="bz">
          <el-input v-model="form.bz" placeholder="请输入配置说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRedisconfig, getRedisconfig, testRedisconfig, updateRedisconfig } from "@/api/system/redisconfig";

export default {
  name: "Redisconfig",
  data() {
    let confirmPwd = (rule, value, callback) => {
        if(value === undefined || value === null || value === "") {
          callback(new Error("请再次输入密码！"));
        } else if(value !== this.form.pwd) {
          callback(new Error("两次输入密码不一致，请重新输入！"));
        } else {
          callback();
        }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // redis配置表格数据
      redisconfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分页参数
      pageParams: {
        current: 1,
        size: 10,
      },
      // 查询参数
      queryParams: {
        redisId: null,
        ip: null,
        bz: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ip: [
          { required: true, message: "访问地址不能为空！", trigger: "blur" }
        ],
        port: [
          { required: true, message: "端口不能为空！", trigger: "blur" }
        ],
        pwd: [
          { required: true, message: "密码不能为空！", trigger: "blur" }
        ],
        qrpwd: [
          { required: true, validator: confirmPwd, trigger: "blur" }
        ],
        maxTotal: [
          { required: true, message: "最大连接数不能为空！", trigger: "blur" }
        ],
        maxIdle: [
          { required: true, message: "最小连接数不能为空！", trigger: "blur" }
        ],
        maxWaitMillis: [
          { required: true, message: "连接空闲时间不能为空！", trigger: "blur" }
        ],
        timeout: [
          { required: true, message: "请求超时时间不能为空！", trigger: "blur" }
        ],
        bz: [
          { required: true, message: "配置说明不能为空！", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询redis配置列表 */
    getList() {
      this.loading = true;
      listRedisconfig(this.pageParams,this.queryParams).then(response => {
        this.redisconfigList = response.page.records;
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        redisId: null,
        ip: null,
        port: null,
        pwd: null,
        qrpwd: null,
        maxTotal: null,
        maxIdle: null,
        maxWaitMillis: null,
        timeout: null,
        bz: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      var redisIdUpdate = row.redisId;
      if(row.isTrusted){
        redisIdUpdate = this.ids[0];
      }
      this.queryParams["redisId"] = redisIdUpdate;
      getRedisconfig(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改redis配置";
        this.queryParams["redisId"] = null;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.redisId != null) {
            updateRedisconfig(this.form).then(response => {
              if(response.code==='200'&&response.data>0) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }else {
                this.$modal.msgError("修改失败");
                this.open = false;
              }
            });
          }
        }
      });
    },
    /** 测试配置操作 */
    handleTest(row) {
      var redisIds = this.ids;
      if(row.redisId){
        redisIds = new Array();
        redisIds.push(row.redisId);
      }
      testRedisconfig(redisIds).then(response => {
        if(response.code==='200') {
          this.$modal.msgSuccess(row.redisId + "测试通过");
        }else{
          this.$modal.msgError(response.msg);
        }
      });
    }
  }
};
</script>
